<template>
	<view>
		<view class="plr40 content">
		<!-- 	<view class="">
				<image src="`${$domain.ossapi}logo.png`" mode="" style="width: 140upx;height: 140upx;"></image>
			</view> -->
			<!-- <view class="ft20 mt20">欢迎使用</view>
			<view class="ft24 mt10 bold">岐黄客 - 医生端</view>
			<view class="mt50">
				<view class="bgInput radius10 plr20">
					<input type="text" v-model="form.username" class="h55" placeholder="请输入账号" placeholder-class="placeholderStyle" />
				</view>
				<view class="bgInput radius10 plr20 mt20">
					<input type="text" v-model="form.password" class="h55" placeholder="请输入密码" placeholder-class="placeholderStyle" />
				</view>
			</view>
			<view class="mt20 align-center justify-between">
				<view class="light" @click="navTo('sign')">注册新账号</view>
				<view class="" @click="navTo('forgetPwd')">忘记密码？</view>
			</view> -->
			<!-- <button class="" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" >微信授权登录</button> -->
            
			<button class="submit" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"    >手机号快捷登录</button>
            <uv-popup ref="popup" mode="bottom" @change="change">
                
                <view class="popcontent plr20 ptb20">
                    <view class="title"> 请设置您的基本信息  </view>
                    <view class="justify-start align-center ">
                        <text class="mr20">设置头像</text> <button class="avatar-box" open-type="chooseAvatar" @chooseavatar="chooseAvatar" type="default" >
                            <image v-if="form.avatar" :src="form.avatar" mode="aspectFit"></image>
                            
                         </button>
                    </view>
                    <view class="justify-start align-center mt15">
                          <text class="mr20">设置昵称</text>   <input id="nickname-input" v-model="form.nickname" type="nickname" class="weui-input" placeholder="请输入昵称"/>
                    </view>
                    <view @click="login"  class="submit  mt10">
                        确定
                    </view>
                </view>
            			
            </uv-popup>

		</view>
	</view>
</template>

<script>
	import operate from '@/common/operate.js'
	export default {
		data() {
			return {
                    form:{
                        openid:"",
                        nickname:"",
                        avatar:"",
                        phone:""
                    },
                    sessionKey:""
                    
			}
		},
        onLoad() {
          this.getSessionKey()  
        },
        mounted() {
          // this.$refs.popup.open()
        },
		methods: {
            async getSessionKey(){
                let vm =this
                uni.login({
                	provider: 'weixin',
                	"onlyAuthorize": true,
                	success: async function (data) {
                		await vm.$api.wechatSession({code:data.code}).then(res => {
                            
                			if (res.code == '1' && res.data) {
                				 vm.sessionKey = res.data.session_id
                				 uni.setStorageSync('sessionKey', res.data.session_id)
                			}
                		})
                		
                	},
                	fail: function (err) {
                		reject()
                		console.log('登录授权失败', err.code)
                	},
                });
            },
            login(){
				let vm =this
				uni.createSelectorQuery().in(this) // 注意这里要加上 in(this)  
				    .select("#nickname-input")  
				    .fields({  
				        properties: ["value"],  
				    })  
				    .exec((res) => {  
				        const nickName = res?.[0]?.value  
				        console.log('昵称', nickName)  
						vm.form.nickname= nickName
						vm.$api.login(vm.form).then(res=>{
						    console.log(res)
						        if(res.code==200){
									uni.setStorageSync('logintoken', res.data.token)
									uni.setStorageSync('userId', res.data.userId)
									vm.getUserInfo(res.data.userId)
						         
						        }
						})
				    })
                
            },
			async getUserInfo(userid){
				let vm =this
			 const {data} =  await this.$api.userinfo({user_id:userid})
			 console.log(data)
			 
			 vm.$utils.msg('登录成功')
			 uni.setStorageSync('userInfo', data)
			 setTimeout(()=>{
				 uni.switchTab({
				     url:'/pages/tabbar/mine'
				 })
			 },800)
			 
			  
			},
            getPhoneNumber(event){
                
                    let vm =this
                   this.$api.wechatLogin({
                                           event:'login',
                                          code: event.detail.code,
                                          iv: event.detail.iv,
                                          encryptedData: event.detail.encryptedData,
                                          session_id:vm.sessionKey|| uni.getStorageSync("session_id"), 
                                       }).then(res=>{
                                           console.log(res)
                                               if(res.code==1){
                                                   this.$utils.msg(res.msg)
                                                   uni.setStorageSync('logintoken', res.data.token)
                                                   setTimeout(()=>{
                                                       if(uni.getStorageSync('tableId')){
                                                           uni.navigateBack()
                                                       }else{
                                                           uni.reLaunch({
                                                               url:'/pages/tabbar/index'
                                                           })
                                                       }
                                                     
                                                   },800)
                                               }
                                       })
            },
            async chooseAvatar(e) {
            	const {avatarUrl} = e.detail
				console.log(avatarUrl)
            	const result = await this.$api.uploadImg({filePath:avatarUrl})
            	console.log(result)
            	
            },
          
			
			

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.content {
		padding-top: 150upx;
	}
  
    .avatar-box {
    	width: 96rpx;
    	height: 96rpx;
    	background: transparent;
        border: none;
            border-radius: 50%;
    	image {
    		width: 100%;
    		height: 100%;
    		border-radius: 50%;
    	}
        &::after{
            display: none;
        }
    }
    .title{
        font-size: 32rpx;
        text-align: center;
        margin: 30rpx auto 60rpx;
        font-weight: 600;
    }
    .submit{
        width: 600rpx;
        height: 90rpx;
      
        margin:  52rpx auto  40rpx;
        background: #2CC6A1;
        border-radius: 8rpx;
        font-size: 32rpx;
        color: #FFFFFF;
        text-align: center;
        line-height: 90rpx;
    }
    
</style>